import { configureStore } from '@reduxjs/toolkit'
import allReducers from './reducers'
//导入持久化所需要的插件
import { persistStore, persistReducer } from 'redux-persist'
//导入本地存储插件，可选storage，cookie，session等
import storage from 'redux-persist/lib/storage'
import { APP_ID } from '@/config'

// 持久化配置
const persistConfig = {
  key: APP_ID,
  storage,
  // whitelist: [users], // 需要持久化保存的模块，默认保存所有模块（语义：白名单）
  // blacklist: [], // 不需要持久化保存的模块，默认不排除任何模块（语义：黑名单）
}

//创建持久化后的reducer
const persistedReducer = persistReducer(persistConfig, allReducers)

const store = configureStore({
  reducer: persistedReducer,
  devTools: true,
  // 配置中间件：如果使用redux-persist，则需要设置为false，否则控制台报错（非序列化数据）
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false,
    }),
})
//创建持久化后的store
const persistor = persistStore(store)

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

export { store, persistor }
